package com.example.experiment2;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;

import static java.lang.Thread.sleep;

public class UC3 extends AppCompatActivity {

    FrameLayout fmout;
    ImageView img[] = new ImageView[5];
    Button back_btn;
    private int imgIndex=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_uc3);
        fmout = findViewById(R.id.UC3_frame);
        img[0] = findViewById(R.id.img1);
        img[1] = findViewById(R.id.img2);
        img[2] = findViewById(R.id.img3);
        img[3] = findViewById(R.id.img4);
        img[4] = findViewById(R.id.img5);
        changeImg();
        back_btn=findViewById(R.id.UC3_back);
        back_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void changeImg(){
        final Handler handler=new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                fmout.removeView(img[msg.what]);
                fmout.addView(img[msg.what]);
                return true;
            }
        });

        Timer timer=new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if(imgIndex==5)
                    imgIndex=0;
                Message message=new Message();
                message.what=imgIndex;
                handler.sendMessage(message);
                imgIndex++;
            }
        },0,1500);

        /*new Thread(new Runnable() {
            @Override
            public void run() {
                int imgIndex=0;
                while (true){
                    try {
                        Thread.sleep(1500);
                    } catch (InterruptedException e) {e.printStackTrace();}
                    if(imgIndex==5)
                        imgIndex=0;
                    Message message=new Message();
                    message.what=imgIndex;
                    handler.sendMessage(message);
                    imgIndex++;
                }
            }
        }).start();*/
    }


}
